package cn.qin.xhangapi.config;

import cn.qin.xhangapi.util.qiniuyun.QiniuProperties;
import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 七牛配置类
 *
 * @author 多仔ヾ
 */
@Configuration
@EnableConfigurationProperties(QiniuProperties.class)
public class QiniuConfigure {

    @Resource
    QiniuProperties qiniuProperties;

    /**
     * 配置自己空间所在的区域，默认为华南机房
     *
     * @author 多仔ヾ
     * @return com.qiniu.storage.Configuration
     */
    @Bean
    public com.qiniu.storage.Configuration qiniuConfig() {
        return new com.qiniu.storage.Configuration(Zone.huanan());
    }

    /**
     * 构建七牛上传工具实例
     *
     * @author 多仔ヾ
     * @return com.qiniu.storage.UploadManager
     */
    @Bean
    public UploadManager uploadManager() {
        return new UploadManager(qiniuConfig());
    }

    /**
     * 构建七牛认证信息实例
     *
     * @author 多仔ヾ
     * @return com.qiniu.util.Auth
     */
    @Bean
    public Auth auth() {
        return Auth.create(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey());
    }

    /**
     * 构建七牛空间管理实例
     *
     * @author 多仔ヾ
     * @return com.qiniu.storage.BucketManager
     */
    @Bean
    public BucketManager bucketManager() {
        return new BucketManager(auth(), qiniuConfig());
    }

}